package com.jzo2o.health.controller.user;

import com.jzo2o.api.foundations.dto.request.OrdersPayReqDTO;
import com.jzo2o.api.orders.ConsumerOrdersApi;
import com.jzo2o.api.orders.dto.request.OrderCancelReqDTO;
import com.jzo2o.api.trade.enums.PayChannelEnum;
import com.jzo2o.common.model.CurrentUserInfo;
import com.jzo2o.common.utils.JwtTool;
import com.jzo2o.health.config.JwtConfiguration;
import com.jzo2o.health.model.domain.Orders;
import com.jzo2o.health.model.dto.request.OrdersPageQueryReqDTO;
import com.jzo2o.health.model.dto.request.PlaceOrderReqDTO;
import com.jzo2o.health.model.dto.response.OrdersPayResDTO;
import com.jzo2o.health.model.dto.response.PlaceOrderResDTO;
import com.jzo2o.health.service.IOrdersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author itcast
 */
@RestController("userOrdersController")
@RequestMapping("/user/orders")
@Api(tags = "用户端 - 下单支付相关接口")
public class OrdersController{

    @Autowired
    private IOrdersService ordersService;


    @Autowired
    JwtTool jwtTool;

    @ApiOperation("下单接口")
    @PostMapping("/place")
    public PlaceOrderResDTO place(@RequestBody PlaceOrderReqDTO placeOrderReqDTO,@RequestHeader("Authorization")String token) {
        CurrentUserInfo currentUserInfo = jwtTool.parseToken(token);
        Long id = currentUserInfo.getId();
        return ordersService.place(placeOrderReqDTO,id);
    }

    @PutMapping("/pay/{id}")
    @ApiOperation("订单支付")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单id", required = true, dataTypeClass = Long.class),
            @ApiImplicitParam(name = "tradingChannel", value = "支付渠道：ALI_PAY、WECHAT_PAY", required = true, dataTypeClass = PayChannelEnum.class),
    })
    public OrdersPayResDTO pay(@PathVariable("id") Long id, @RequestParam("tradingChannel") PayChannelEnum tradingChannel) {

        return ordersService.pay(id,tradingChannel);
    }

    @GetMapping("/pay/{id}/result")
    @ApiOperation("查询订单支付结果")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单id", required = true, dataTypeClass = Long.class)
    })
    public OrdersPayResDTO payResult(@PathVariable("id") Long id) {
        return ordersService.getPayResultFromTradServer(id);
    }

    @PutMapping("/cancel")
    @ApiOperation("用户端取消未支付订单")
    public OrderCancelReqDTO cancel(@RequestBody OrderCancelReqDTO orderCancelReqDTO,@RequestHeader("Authorization")String token) {
        CurrentUserInfo currentUserInfo = jwtTool.parseToken(token);
        Long id = currentUserInfo.getId();
        return ordersService.cancel(orderCancelReqDTO,id);
    }

    @PutMapping("/refund")
    @ApiOperation("用户端订单退款")
    public OrderCancelReqDTO refund(@RequestBody OrderCancelReqDTO orderCancelReqDTO) {
        return ordersService.refund(orderCancelReqDTO);
    }



//    @GetMapping("/page")
//    @ApiOperation("用户端分页查询订单")
//    public List<Orders> pages(@RequestBody OrdersPageQueryReqDTO ordersPageQueryReqDTO) {
//        return ordersService.page(ordersPageQueryReqDTO);
//    }

//    @GetMapping("/{id}")
//    @ApiOperation("用户端查询订单详情")
//    public Orders getById(@PathVariable("id") Long id) {
//        return ordersService.getById(id);
//    }

}
